import wxValidate from 'wechat-validate'
Page({
  behaviors:[wxValidate],
  data: {
    currentDate: new Date().getTime(),//当前时间
    appointment:"",//时间戳
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    houseId:'',
    houseName:'',
    repairItemId:'',
    repairName:'',
    attachment: [],//上传图片
    mobile: '',
    description: '',
  },
  //验证规则
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  //提交表单数据
 async submitForm(){
    if(!this.validate())return
     // 提取接口需要的数据
     const {houseId,repairItemId,mobile,appointment,description,attachment,}=this.data
     //调用接口
     const {code} =await wx.http.post('/repair',{houseId,repairItemId,mobile,appointment,description,attachment,})
     if(code !==10000) return wx.utils.toast('在线报修失败')
    //  跳转路由
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
  //在页面一渲染就要触发数据
  onLoad(){
    this.getHouseList()
    this.getrepairItem()
  },
  // 获取房屋列表（必须是通过审核的房屋）
  async getHouseList() {
    // 调用接口
    const { code, data: houseList } = await wx.http.get('/house')
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ houseList })
  },
  //获取维修列表
  async getrepairItem(){
    const {code,data:repairItem}=await wx.http.get('/repairItem')
    if(code!==10000)return wx.utils.toast()
    this.setData({
      repairItem
    })
  },
  //选择房屋信息
  selectHouseInfo(ev){
   this.setData({
    houseId:ev.detail.id,
    houseName:ev.detail.name
   })
  },
  //选择维修信息
  selectRepair(ev){
  this.setData({
    repairItemId:ev.detail.id,
    repairName:ev.detail.name
  })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  //关闭选择日期弹框
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  //选择日期
  selectDateInfo(ev){
  this.setData({ 
    appointment:wx.utils.dataFormat(ev.detail),
    dateLayerVisible: false
   })
  },
  //上传图片
  uploadPicture(ev){
    const { file } = ev.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: wx.http.baseURL+'/upload', 
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token,
      },
      success:(result)=> {
        // 上传完成需要更新 fileList
       console.log(result);
       // // 处理返回的 json 数据
       const data=JSON.parse(result.data)
       if(data.code !==10000) return wx.utils.toast('文件上传失败')
      // 先获取原来已经上传的图片
      const {attachment} =this.data
      //获取新上传的图片
      attachment.push(data.data)
      this.setData({
        attachment
      })
      },
    });
  },
 
})
